//
//  Carpenterworm.swift
//  Dynamic
//
//  Created by Yinyin Zeng on 30/5/20.
//  Copyright © 2020 MaoYeYeDeJiangCuCha. All rights reserved.
//

import UIKit
//毛毛虫
class Carpenterworm: UIView {
    
    var animator = UIDynamicAnimator()
    var attachAciton:UIAttachmentBehavior?
    var attachAcitonaTwo:UIAttachmentBehavior?
    var wormViewArr:[UIView] = []
    
    
    override init(frame: CGRect) {
        super.init(frame:frame)
        
        //创建身体
        for i in 0..<9 {
            
            let wormW = 30
            let wormH = wormW
            let wormX = i * wormW
            let wormY = 100
            
            let wormView = UIView.init(frame: CGRect(x: wormX, y: wormY, width: wormW, height: wormH))
            wormView.backgroundColor = .lightGray
            wormView.layer.cornerRadius = CGFloat(wormW) * 0.5
            wormView.layer.masksToBounds = true
            
            if i == 8{
                wormView.backgroundColor = .black
                wormView.frame = CGRect(x: wormX, y: wormY - wormH / 2, width: 2 * wormW, height:2 * wormH)
                wormView.layer.cornerRadius = CGFloat(wormW)
                
                let panGesture = UIPanGestureRecognizer(target: self, action: #selector(panGestureAction))
                wormView.addGestureRecognizer(panGesture)
                wormView.tag = 8
                animator = UIDynamicAnimator.init(referenceView: self)
                
            }
            wormViewArr.append(wormView)
            self.addSubview(wormView)
            
        }
        
        for (index,wormView) in wormViewArr.enumerated() {
            //附着
            if index < wormViewArr.count - 1{
                attachAcitonaTwo = UIAttachmentBehavior.init(item:wormView, attachedTo: wormViewArr[index+1])
                animator.addBehavior(attachAcitonaTwo!)
            }
        }
        //重力
        let gravity = UIGravityBehavior(items: wormViewArr)
        //碰撞
        let collision = UICollisionBehavior(items: wormViewArr)
        collision.translatesReferenceBoundsIntoBoundary = true
        animator.addBehavior(gravity)
        animator.addBehavior(collision)
        
        
    }

    @objc func panGestureAction(sender:UIPanGestureRecognizer){

        let pLocation = sender.location(in: self)

        if (attachAciton == nil){
            let attach = UIAttachmentBehavior.init(item: sender.view!, attachedToAnchor: pLocation)
            self.attachAciton = attach
            animator.addBehavior(attachAciton!)
        }
        attachAciton?.anchorPoint = pLocation
        
        if sender.state == .ended{
            animator.removeBehavior(attachAciton!)
            attachAciton = nil
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /*
     // Only override draw() if you perform custom drawing.
     // An empty implementation adversely affects performance during animation.
     override func draw(_ rect: CGRect) {
     // Drawing code
     }
     */
    
}
